package router

import (
	"bluebell/controller"
	_ "bluebell/docs"
	"bluebell/logger"
	"bluebell/middlewares"
	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
	//"github.com/swaggo/gin-swagger/swaggerFiles"
	"net/http"
)

// SetupRouter 路由
func SetupRouter(mode string) *gin.Engine {
	if mode == gin.ReleaseMode {
		gin.SetMode(gin.ReleaseMode) // gin设置成发布模式
	}

	r := gin.New()
	//r.Use(logger.GinLogger(), logger.GinRecovery(true), middlewares.RateLimitMiddleware(2*time.Second, 1))
	r.Use(logger.GinLogger(), logger.GinRecovery(true), middlewares.Cors())

	// 静态资源服务：将 static/ 目录下文件映射为 http://localhost:8085/static/xxx
	r.Static("/static", "./static")

	r.LoadHTMLFiles("./templates/index.html")

	r.GET("/", func(c *gin.Context) {
		c.HTML(http.StatusOK, "index.html", nil)
	})
	// 支持头像和图片评论上传
	//r.POST("/upload/image", controller.UploadImageHandler2)
	r.GET("/ping", func(c *gin.Context) {
		c.String(http.StatusOK, "pong")
	})
	// 在适当的位置添加路由

	//r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	//RESTFUL api  post get delete put
	//127.0.0.1：8080/api/v1/signup
	v1 := r.Group("/api/v1")
	{
		// 注册
		v1.POST("/signup", controller.SignUpHandler)
		// 登录
		v1.POST("/login", controller.LoginHandler)
		// 短信验证码登录
		v1.POST("/loginSMS", controller.LoginSMSHandler)
		// 根据时间或分数获取帖子列表
		v1.GET("/posts2", controller.GetPostListHandler2)
		v1.GET("/posts", controller.GetPostListHandler)
		v1.GET("/community", controller.CommunityHandler)
		v1.GET("/community/id/:id", controller.CommunityDetailHandler)
		//v1.GET("/community/name/:name", controller.CommunityListByNamehanlder)
		// v1.GET("/post/:id", controller.GetPostDetailHandler)
		v1.GET("/select", controller.GetPostBySelect)
		// 新增评论路由
		v1.GET("/comment/list", controller.GetCommentListHandler)
		v1.GET("/stats/:post_id", controller.GetPostStatsHandler)
		v1.GET("/posts/search", controller.GetPostsByKeywordHandler)
		// 根据社区ID获取帖子列表（新增）
		v1.GET("/community/:id/posts", controller.GetCommunityPostListHandler)
	}

	auth := v1.Group("/")

	auth.Use(middlewares.JWTAuthMiddleware())
	{
		//	r.POST("/user/avatar", controller.UpdateAvatarHandler) //保存用户头像
		auth.POST("/upload/image", controller.UploadImageHandler) //保存用户头像

		// 收藏帖子：使用POST方法，路径包含帖子ID
		auth.POST("/collect/:post_id", controller.CollectPostHandler)
		// 取消收藏：使用DELETE方法，路径包含帖子ID
		auth.DELETE("/uncollected/:post_id", controller.UncollectPostHandler)
		auth.POST("/toggle_collect/:post_id", controller.ToggleCollectHandler)
		auth.GET("/collect_status/:post_id", controller.GetCollectStatusHandler)
		// 获取用户收藏列表：使用GET方法，路径明确为收藏列表
		auth.GET("/collections", controller.GetCollectedPostListHandler)

		// 添加认证的路由
		auth.GET("/post/:id", controller.GetPostDetailHandler) //

		// 用户头像上传
		// auth.POST("/user/:user_id/avatar", controller.PostAvatar)
		auth.GET("user/public_posts", controller.GetPostsByUserIDHandler) // 用户发布的帖子列表
		auth.POST("user/delete_public_posts", controller.DeletePostsByUserIDAndPostIDHandler)
		// 发布帖子
		auth.POST("/post", controller.CreatePostHandler)
		auth.POST("/like", controller.LikePostHandler)                       // 点赞 ///:post_id
		auth.DELETE("/like/:post_id", controller.UnlikePostHandler)          // 取消点赞
		auth.POST("/like/toggle", controller.ToggleLikeHandler)              // 切换状态（建议也改为路径参数）
		auth.GET("/like/status/:post_id", controller.CheckLikeStatusHandler) // 检查状态
		// 个人页面
		auth.GET("/userPage", controller.GetUserPage)
		// 删除帖子
		auth.DELETE("/deleteV1", controller.DeletePost)
		// 新增受保护的评论创建路由
		auth.POST("/comment", controller.CreateCommentHandler)

		// 用户观看历史
		auth.GET("/user/history", controller.GetHistoryListHandler)
		// 删除用户观看历史
		auth.DELETE("/user/history", controller.DeleteHistoryHandler)
	}

	manager := r.Group("/manager", middlewares.JWTAuthMiddleware(), middlewares.AuthManager())
	{
		// 删除帖子
		manager.DELETE("/deleteRoot", controller.DeletePost)
		// 删除用户
		manager.DELETE("/deleteUser", controller.DeleteUser)
		// 置顶帖子
		//manager.POST("/postTop", controller.PostTop)
		// 删除用户头像
		//manager.DELETE("/deleteAvatar", controller.DeleteAvatar)
	}
	pprof.Register(r) // 注册pprof相关路由

	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"msg": "404",
		})
	})
	return r
}
